package com.government.member.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.area.domain.ParkArea;
import com.government.area.mapper.ParkAreaMapper;
import com.government.common.core.domain.entity.SysUser;
import com.government.member.domain.Vip;
import com.government.member.mapper.VipMapper;
import org.springframework.transaction.annotation.Transactional;
import com.government.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.member.mapper.MemberInfoMapper;
import com.government.member.domain.MemberInfo;
import com.government.member.service.IMemberInfoService;

import static com.government.common.utils.SecurityUtils.getLoginUser;

/**
 * 会员信息Service业务层处理
 *
 * @author system
 * @date 2023-09-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemberInfoServiceImpl implements IMemberInfoService {
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private ParkAreaMapper parkAreaMapper;

    /**
     * 查询会员信息
     *
     * @param memberId 会员信息主键
     * @return 会员信息
     */
    @Override
    public MemberInfo selectMemberInfoByMemberId(Long memberId) {
        return memberInfoMapper.selectMemberInfoByMemberId(memberId);
    }

    /**
     * 查询会员信息列表
     *
     * @param memberInfo 会员信息
     * @return 会员信息
     */
    @Override
    public List<MemberInfo> selectMemberInfoList(MemberInfo memberInfo) {
        return memberInfoMapper.selectMemberInfoList(memberInfo);
    }

    /**
     * 新增会员信息
     *
     * @param memberInfo 会员信息
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo) {
        memberInfo.setCreateTime(DateUtils.getNowDate());
        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改会员信息
     *
     * @param memberInfo 会员信息
     * @return 结果
     */
    @Override
    public int updateMemberInfo(MemberInfo memberInfo) {

//        if (StrUtil.isNotBlank(memberInfo.getVipType())) {
//            List<Vip> vipList = vipMapper.selectList(Wrappers.<Vip>lambdaQuery().eq(Vip::getUid, memberInfo.getMemberId())
//                    .eq(Vip::getStatus,"1"));
//            if(CollUtil.isNotEmpty(vipList)) {
//                for (Vip vip : vipList) {
//                    if(vip.getVipType() == 1) {
//
//                    }
//                }
//            }
//        }
        memberInfo.setUpdateTime(DateUtils.getNowDate());
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }

    /**
     * 批量删除会员信息
     *
     * @param memberIds 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberIds(String[] memberIds) {
        return memberInfoMapper.deleteMemberInfoByMemberIds(memberIds);
    }

    /**
     * 删除会员信息信息
     *
     * @param memberId 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberId(Long memberId) {
        MemberInfo memberInfo = memberInfoMapper.selectById(memberId);
        memberInfo.setIsDeleted("1");
        memberInfo.setUpdateTime(new Date());
        memberInfo.setUpdateBy(String.valueOf(getLoginUser().getUserId()));
        return memberInfoMapper.updateById(memberInfo);
    }

    /**
     * 通过openId获取会员信息信息
     *
     * @param openId
     * @return 结果
     */
    @Override
    public MemberInfo selectMemberInfoByOpenId(String openId) {
        return memberInfoMapper.selectOne(Wrappers.<MemberInfo>lambdaQuery().eq(MemberInfo::getWechatOpenId, openId));
    }

    /**
     * 绑定园区信息
     *
     * @param parkAreaIds
     * @return 结果
     */
    @Override
    public void bindPark(List<String> parkAreaIds) {

        SysUser user  = getLoginUser().getUser();
        MemberInfo memberInfo = memberInfoMapper.selectById(user.getUserId());
        
        // 如果会员信息不存在，则创建一个新的会员信息记录
        if (memberInfo == null) {
            memberInfo = new MemberInfo();
            memberInfo.setMemberId(user.getUserId());
            memberInfo.setUserName(user.getUserName());
            memberInfo.setNickName(user.getNickName());
            memberInfo.setCreateTime(new Date());
            memberInfo.setUpdateTime(new Date());
            
            // 检查用户是否有VIP信息
            List<Vip> vipList = vipMapper.selectList(Wrappers.<Vip>lambdaQuery().eq(Vip::getUid, user.getUserId()));
            if (CollUtil.isNotEmpty(vipList)) {
                // 如果有VIP信息，设置相关字段
                Vip vip = vipList.get(0); // 取第一个VIP记录
                memberInfo.setVipType(String.valueOf(vip.getVipType()));
                memberInfo.setVipName(getVipName(vip.getVipType()));
            }
            
            memberInfoMapper.insert(memberInfo);
        }

        List<ParkArea> parkAreaList = parkAreaMapper.selectList(Wrappers.<ParkArea>lambdaQuery());
        Map<Long, String> parkAreaMap = parkAreaList.stream().collect(Collectors.toMap(ParkArea::getId, ParkArea::getName));

        // 园区
        if (CollUtil.isNotEmpty(parkAreaIds)) {
            if (parkAreaIds.size() > 0) {
                Long park1 = Long.parseLong(parkAreaIds.get(0));
                memberInfo.setPark1(park1);
                memberInfo.setLastPark(park1);
                memberInfo.setParkName1(parkAreaMap.get(park1));
                memberInfo.setLastParkName(parkAreaMap.get(park1));
            }
            if (parkAreaIds.size() > 1) {
                Long park2 = Long.parseLong(parkAreaIds.get(1));
                memberInfo.setPark2(park2);
                memberInfo.setLastPark(park2);
                memberInfo.setParkName2(parkAreaMap.get(park2));
                memberInfo.setLastParkName(parkAreaMap.get(park2));
            }
            if (parkAreaIds.size() > 2) {
                Long park3 = Long.parseLong(parkAreaIds.get(2));
                memberInfo.setPark3(park3);
                memberInfo.setLastPark(park3);
                memberInfo.setParkName3(parkAreaMap.get(park3));
                memberInfo.setLastParkName(parkAreaMap.get(park3));
            }
            if (parkAreaIds.size() > 3) {
                Long park4 = Long.parseLong(parkAreaIds.get(3));
                memberInfo.setPark4(park4);
                memberInfo.setLastPark(park4);
                memberInfo.setParkName4(parkAreaMap.get(park4));
                memberInfo.setLastParkName(parkAreaMap.get(park4));
            }
            if (parkAreaIds.size() > 4) {
                Long park5 = Long.parseLong(parkAreaIds.get(4));
                memberInfo.setPark5(park5);
                memberInfo.setLastPark(park5);
                memberInfo.setParkName5(parkAreaMap.get(park5));
                memberInfo.setLastParkName(parkAreaMap.get(park5));
            }
            if (parkAreaIds.size() > 5) {
                Long park6 = Long.parseLong(parkAreaIds.get(5));
                memberInfo.setPark6(park6);
                memberInfo.setLastPark(park6);
                memberInfo.setParkName6(parkAreaMap.get(park6));
                memberInfo.setLastParkName(parkAreaMap.get(park6));
            }
            memberInfo.setParkAreaId(String.join(",", parkAreaIds));
            memberInfo.setUpdAreaTime(new Date());
        }
        memberInfo.setUpdateTime(new Date());
        memberInfoMapper.updateById(memberInfo);
    }
    
    /**
     * 根据VIP类型获取VIP名称
     * @param vipType VIP类型
     * @return VIP名称
     */
    private String getVipName(int vipType) {
        switch (vipType) {
            case 1:
                return "vip";
            case 2:
                return "svip";
            case 3:
                return "企业vip";
            default:
                return "注册用户";
        }
    }
}
